using Digitalmes.Application.Sys.Configs;
using Digitalmes.Application.Sys.Configs.Commands;
using Digitalmes.Application.Sys.Configs.Queries;

namespace Digitalmes.WebApi.Endpoints.Sys;

/// <summary>
/// 系统配置终结点。
/// </summary>
public sealed class ConfigEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "sys", "config")
            .RequireAuthorization()
            .MapGet(GetConfigPagedListAsync, "list-page")
            .MapPost(CreateConfigAsync)
            .MapPut(UpdateConfigAsync, "{id}")
            .MapDelete(DeleteConfigAsync, "{id}");
    }

    [EndpointDescription("分页查询系统配置")]
    public async Task<IApiResult> GetConfigPagedListAsync(ISender sender, [AsParameters] GetConfigListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建系统配置")]
    public async Task<IApiResult> CreateConfigAsync(ISender sender, SysConfigDto dto)
    {
        var result = await sender.Send(new CreateConfigCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新系统配置")]
    public async Task<IApiResult> UpdateConfigAsync(ISender sender, long id, SysConfigDto dto)
    {
        var result = await sender.Send(new UpdateConfigCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除系统配置")]
    public async Task<IApiResult> DeleteConfigAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteConfigCommand(id));
        return result.ToApiResult();
    }
}
